package com.cskaoyan.gateway.controller.shopping;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
//import com.mall.user.annotation.Anonymous;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.yetus.audience.InterfaceAudience;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@Anoymous
public class ProductController {

    @Reference(timeout = 3000, check = false)
    IProductService productService;
    @Reference(timeout = 3000, check = false)
    IProductCateService iProductCateService;

    /*商品所有类目*/

    @ApiOperation("查询商品类目")
    @GetMapping("/shopping/categories")
    public ResponseData productAllCat() {
        AllProductCateRequest allProductCateRequest = new AllProductCateRequest();
        allProductCateRequest.setSort("ASC ");
        AllProductCateResponse allProductCates = iProductCateService.getAllProductCate(allProductCateRequest);

        ResponseData<Object> objectResponseData = new ResponseData<>();

        objectResponseData.setSuccess(true);
        objectResponseData.setMessage("success");
        objectResponseData.setCode(Integer.parseInt(allProductCates.getCode()));
        objectResponseData.setResult(allProductCates.getProductCateDtoList());
        objectResponseData.setTimestamp(new Date().getTime());
        return objectResponseData;

    }



    /**
     * 查询商品详情
     * Token no
     */
    @ApiOperation("查询商品详情")
    @GetMapping("shopping/product/{id}")
    public ResponseData product(@PathVariable long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse response = productService.getProductDetail(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @ApiOperation("分页查询")
    @GetMapping("/shopping/goods")
    public ResponseData ProductFind(AllProductRequest allProductRequest) {
        AllProductResponse allProduct = productService.getAllProduct(allProductRequest);

        A a = new A();
        a.setData(allProduct.getProductDtoList());
        a.setTotal(allProduct.getTotal());

        return new ResponseUtil().setData(a);
    }

    @Data
    private class A {
        List<ProductDto> data;
        Long total;
    }


    @ApiOperation("推荐")
    @GetMapping("/shopping/recommend")
    public ResponseData productRecommd() {
        RecommendResponse recommendGoods = productService.getRecommendGoods();
        Set<PanelDto> panelContentItemDtos = recommendGoods.getPanelContentItemDtos();
        Object[] objects = panelContentItemDtos.toArray();

        return new ResponseUtil().setData(objects);
    }





}
